package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.excpetion.CustException;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("ApUnlikeBehaviorService")
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    /**
     * 保存 或 取消 不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null)
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);

        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());


        // 用户执行的是不喜欢
        if (dto.getType() == 0) {
            ApUnlikesBehavior behavior = new ApUnlikesBehavior();
            behavior.setEntryId(String.valueOf(apBehaviorEntry.getId()));
            behavior.setType(ApUnlikesBehavior.Type.UNLIKE.getCode());
            behavior.setCreatedTime(new Date());
            behavior.setArticleId(dto.getArticleId());
            mongoTemplate.save(behavior);

        } else {
            //用户执行的取消不喜欢 ->根据用户Id向不喜欢库删除数据
            mongoTemplate.remove(Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId())), ApUnlikesBehavior.class);
        }
        // 根据用户执行的操作向MQ发送收藏情况消息
        {
            UpdateArticleMess articleMess = new UpdateArticleMess();
            articleMess.setType(UpdateArticleMess.UpdateArticleType.UNLIKES);
            //如果是0 ->点赞操作 ->设置增加属性为1
            articleMess.setAdd(dto.getType() == 1 ? 5 : -5);
            articleMess.setArticleId(dto.getArticleId());
            //发送消息
            rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(articleMess));
        }
        return ResponseResult.okResult();
    }
}
